package com.example.user.customviewdemo.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.example.user.customviewdemo.R;

/**
 * Created by user on 2016/5/6.
 */
public class StepView extends View {

    private int step = 1;
    private final int FIRST_STEP = 1;
    private final int SECOND_STEP = 2;
    private final int THREE_STEP = 3;

    private Paint paint,textPaint;
    private Bitmap firstBmp,secondBmp,thirdBmp;


    public StepView(Context context) {
        super(context);
    }

    public StepView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        //初始化写文字字符串的paint
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(23);
        textPaint.setStyle(Paint.Style.FILL);

        //初始化第一、第二、第三步的bitmap
        firstBmp = mkFirstStep();
        secondBmp = mkSecondStep();
        thirdBmp = mkThirdStep();

    }

    public StepView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawColor(0xff414F65);
        Bitmap bmp = null;
        switch (step){
            case FIRST_STEP:
               bmp =  firstBmp;
                break;
            case SECOND_STEP:
               bmp =  secondBmp;
                break;
            case THREE_STEP:
               bmp =  thirdBmp;
                break;
        }

        //画图
        canvas.drawBitmap(bmp,getWidth()/2-bmp.getWidth()/2,getHeight()/2 - bmp.getHeight()/2,paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;

        if(widthMode == MeasureSpec.EXACTLY){
            width = widthSize;
        }else {
            width = firstBmp.getWidth() + getPaddingLeft() + getPaddingRight();//firstBmm、secondBmp、thridBmp的宽相等
        }

        if(heightMode == MeasureSpec.EXACTLY){
            height = heightSize;
        }else{
            height = firstBmp.getHeight() + getPaddingTop() + getPaddingBottom();
        }

        setMeasuredDimension(width,height);
    }

    /**
     * 得到第一步的bitmap
     * @return
     */
    private Bitmap mkFirstStep(){

        Bitmap background = BitmapFactory.decodeResource(getResources(),R.mipmap.img1);
        Bitmap bmp = Bitmap.createBitmap(background.getWidth(),background.getHeight(), Bitmap.Config.ARGB_4444);
        Canvas canvas1 = new Canvas(bmp);
        canvas1.drawBitmap(background,0,0,null);

        Bitmap bmp1 = mkCircleBmp("步骤一",Color.RED);
        Bitmap bmp2 = mkCircleBmp("步骤二",Color.WHITE);
        Bitmap bmp3 = mkCircleBmp("步骤三",Color.WHITE);

        canvas1.drawBitmap(bmp1,0,0,null);
        canvas1.drawBitmap(bmp2,bmp.getWidth()/2 - bmp2.getWidth()/2,0,null);
        canvas1.drawBitmap(bmp3,bmp.getWidth() - bmp3.getWidth(),0,null);



        return bmp;



    }

    /**
     * 得到第二部的bitmap
     * @return
     */
    private Bitmap mkSecondStep(){

        Bitmap background = BitmapFactory.decodeResource(getResources(),R.mipmap.img2);
        Bitmap bmp = Bitmap.createBitmap(background.getWidth(),background.getHeight(), Bitmap.Config.ARGB_4444);
        Canvas canvas1 = new Canvas(bmp);
        canvas1.drawBitmap(background,0,0,null);

        Bitmap bmp1 = mkCircleBmp("步骤一",Color.RED);
        Bitmap bmp2 = mkCircleBmp("步骤二",Color.RED);
        Bitmap bmp3 = mkCircleBmp("步骤三",Color.WHITE);

        canvas1.drawBitmap(bmp1,0,0,null);
        canvas1.drawBitmap(bmp2,bmp.getWidth()/2 - bmp2.getWidth()/2,0,null);
        canvas1.drawBitmap(bmp3,bmp.getWidth() - bmp3.getWidth(),0,null);

        return bmp;

    }

    /**
     * 得到第三步的bitmap
     * @return
     */
    private Bitmap mkThirdStep(){
        Bitmap background = BitmapFactory.decodeResource(getResources(),R.mipmap.img3);
        Bitmap bmp = Bitmap.createBitmap(background.getWidth(),background.getHeight(), Bitmap.Config.ARGB_4444);
        Canvas canvas1 = new Canvas(bmp);
        canvas1.drawBitmap(background,0,0,null);

        Bitmap bmp1 = mkCircleBmp("步骤一",Color.RED);
        Bitmap bmp2 = mkCircleBmp("步骤二",Color.RED);
        Bitmap bmp3 = mkCircleBmp("步骤三",Color.RED);

        canvas1.drawBitmap(bmp1,0,0,null);
        canvas1.drawBitmap(bmp2,bmp.getWidth()/2 - bmp2.getWidth()/2,0,null);
        canvas1.drawBitmap(bmp3,bmp.getWidth() - bmp3.getWidth(),0,null);

        return bmp;

    }

    /**
     * 创建红色或白色圆形有字的bitmap
     * @param text    如“步骤一”
     * @param color   Color.RED   或者Color.WHITE  根据colre创建红色或者白色的圆形bitmap
     * @return
     */
    private Bitmap mkCircleBmp(String text,int color){
        //截取字符串
        String text1 = text.substring(0,2);
        String text2 = text.substring(2,3);

        Bitmap tempBmp = null;
        switch (color){
            case Color.RED:
                tempBmp = BitmapFactory.decodeResource(getResources(), R.mipmap.image_red);
                textPaint.setColor(Color.WHITE);
                break;
            case Color.WHITE:
                tempBmp = BitmapFactory.decodeResource(getResources(), R.mipmap.image_white);
                textPaint.setColor(Color.BLACK);
                break;
        }

        //创建一个空白bitmap和tempBmp等大小，用空白bitmap生成一个canvas
        Bitmap bmp = Bitmap.createBitmap(tempBmp.getWidth(),tempBmp.getHeight(), Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bmp);
        //画图
        canvas.drawBitmap(tempBmp,0,0,paint);
        //画字符串
        canvas.drawText(text1,tempBmp.getWidth()/2,tempBmp.getHeight()/2,textPaint);
        canvas.translate(0,25);
        canvas.drawText(text2,tempBmp.getWidth()/2,tempBmp.getHeight()/2,textPaint);

        return bmp;
    }



    /**
     * 下一步
     */
    public void nextStep(){
        step++;
        if(step == 4){
            step = 3;
        }
        postInvalidate();
    }

    /**
     * 上一步
     */
    public void preStep(){
        step--;
        if(step == 0){
            step = 1;
        }
        postInvalidate();
    }

}
